﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DeviceInfoManage.BusinessLogicLayer;

public partial class MaintRecordRegister : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("login.aspx");
            }
            BindPerson();
            if (!string.IsNullOrEmpty(Request.QueryString["sn"]))
            {
                hidSN.Value = Request.QueryString["sn"];
                DeviceSerialNumber.Text = hidSN.Value;
                DeviceSerialNumber.ReadOnly = true;
            }

        }
    }

    private void BindPerson()
    {
        ddlMaintPerson.DataSource = (new MaintPerson()).GetMaintPersonName();
        ddlMaintPerson.DataValueField = "maintPersonId";
        ddlMaintPerson.DataTextField = "maintPersonName";
        ddlMaintPerson.DataBind();
        if (ddlMaintPerson.Items.FindByText(Session["UserName"].ToString()) != null)
        {
            ddlMaintPerson.Items.FindByText(Session["UserName"].ToString()).Selected = true;
        }
    }

    /*添加设备信息*/
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        /*建立一个记录对象，然后传入各个字段的属性值，最后调用插入函数执行添加*/
        maintRecordInfo device = new maintRecordInfo();
        device.deviceUser = this.DeviceName.Text;
        device.departmentName = this.DeviceDpt.SelectedValue;
        device.maintTime = DateTime.Parse(this.startTime.Value);
        device.ComTime = Convert.ToDateTime(this.endTime.Value);
        device.errType = this.ddlErrType.SelectedValue;
        device.deviceModel = this.DeviceModel.Text;
        device.deviceSerialNumber = this.DeviceSerialNumber.Text;
        device.propertySerialNumber = this.txtPhone.Text.Trim();
        device.maintPersonId = int.Parse(this.ddlMaintPerson.SelectedValue);
        device.errReason = this.txtErrorReason.Text;
        device.errSolveWay = this.DeviceMemo.Text;
        device.State = "0";
        if (cbisSolve.Checked == true)
        {
            device.IsSolve = "1";
        }
        else
        {
            device.IsSolve = "0";
        }
        /*插入函数返回添加是否成功，如果失败，ErrMessage中保存了错误信息*/
        if (device.Add(device) != -1)
            Response.Write("<script>alert('新维护信息添加成功!');location.href='MaintRecordRegister.aspx';</script>");
        else
            Response.Write("<script>alert('添加失败!');</script>");

    }
}
